package com.loong.musicdemo.ui.find.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Comment;
import com.loong.musicdemo.ui.mine.adapter.MineCommentAdapter;
import com.loong.musicdemo.util.dataDeal.CommentDeal;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class CommentListActivity extends AppCompatActivity {
    private SmartRefreshLayout refreshAllCommentView;
    private SmartRefreshLayout refreshHotCommentView;
    private MineCommentAdapter hotCommentAdapter,allCommentAdapter;
    private TextView all_comment,hot_comment;
    private RecyclerView hot_comment_view,all_comment_view;
    private List<Comment> allComments = new ArrayList<>();
    private List<Comment> hotComments = new ArrayList<>();
    private String commentId="";
    private static final int ALL_REFRESH_COMPLETE = 0;
    private static final int HOT_REFRESH_COMPLETE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        setContentView(R.layout.activity_comment_list);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("评论");
            actionBar.setDisplayHomeAsUpEnabled(true);//若工具栏可用,则显示Home菜单
        }
        getIntentCommentId();
        initView();
        initCommentRecyclerView();
        initRefreshHandler();
        initClickHandler();
        new AllCommentTask().execute();
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home)
            this.finish();
        return super.onOptionsItemSelected(item);
    }
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case ALL_REFRESH_COMPLETE:
                    new AllCommentTask().execute();
                    break;
                case HOT_REFRESH_COMPLETE:
                    new HotCommentTask().execute();
                    break;
            }
        };
    };

    private void initView(){
        hot_comment = findViewById(R.id.hot_comment);
        all_comment = findViewById(R.id.all_comment);
        refreshAllCommentView = findViewById(R.id.smart_refresh_layout_all);
        refreshHotCommentView = findViewById(R.id.smart_refresh_layout_hot);
        hot_comment_view = findViewById(R.id.hot_comment_view);
        all_comment_view = findViewById(R.id.all_comment_view);
    }
    //上拉加载消息处理
    private void initRefreshHandler(){
        refreshAllCommentView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mHandler.sendEmptyMessageDelayed(ALL_REFRESH_COMPLETE,1000);
            }
        });
        refreshAllCommentView.setEnableRefresh(false);
        refreshHotCommentView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mHandler.sendEmptyMessageDelayed(HOT_REFRESH_COMPLETE,1000);
            }
        });
        refreshHotCommentView.setEnableRefresh(false);
    }

    private void initCommentRecyclerView(){
        hotCommentAdapter = new MineCommentAdapter(hotComments,CommentListActivity.this);
        allCommentAdapter = new MineCommentAdapter(allComments,CommentListActivity.this);
        LinearLayoutManager hotManager = new LinearLayoutManager(CommentListActivity.this);
        hotManager.setOrientation(LinearLayoutManager.VERTICAL);
        LinearLayoutManager allManager = new LinearLayoutManager(CommentListActivity.this);
        allManager.setOrientation(LinearLayoutManager.VERTICAL);
        hot_comment_view.setLayoutManager(hotManager);
        hot_comment_view.setAdapter(hotCommentAdapter);
        all_comment_view.setLayoutManager(allManager);
        all_comment_view.setAdapter(allCommentAdapter);
    }
    //获取歌单的id
    private void getIntentCommentId(){
        commentId = getIntent().getStringExtra("id");
    }

    private void initClickHandler(){
        hot_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new HotCommentTask().execute();
                all_comment.setTextColor(Color.rgb(204,204,204));
                hot_comment.setTextColor(Color.BLACK);
                refreshAllCommentView.setVisibility(View.GONE);
                refreshHotCommentView.setVisibility(View.VISIBLE);
            }
        });
        all_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                hot_comment.setTextColor(Color.rgb(204,204,204));
                all_comment.setTextColor(Color.BLACK);
                refreshAllCommentView.setVisibility(View.VISIBLE);
                refreshHotCommentView.setVisibility(View.GONE);
            }
        });
    }

    class AllCommentTask extends AsyncTask<Comment,Void, List<Comment>>{
        @Override
        protected void onPostExecute(List<Comment> comments) {
            super.onPostExecute(comments);
            if(comments.size() == 0){
                refreshAllCommentView.finishLoadmore();
            }else {
                allComments.addAll(comments);
                allCommentAdapter.notifyDataSetChanged();
                refreshAllCommentView.finishLoadmore();
            }

        }

        @Override
        protected List<Comment> doInBackground(Comment... comments) {
            return CommentDeal.getPlayListAllComments(commentId);
        }
    }
    class HotCommentTask extends AsyncTask<Comment,Void, List<Comment>>{
        @Override
        protected void onPostExecute(List<Comment> comments) {
            super.onPostExecute(comments);
            if(comments.size() == 0) {
                refreshHotCommentView.finishLoadmore();
            }else {
                hotComments.addAll(comments);
                hotCommentAdapter.notifyDataSetChanged();
                refreshHotCommentView.finishLoadmore();
            }

        }

        @Override
        protected List<Comment> doInBackground(Comment... comments) {
            return CommentDeal.getPlayListHotComments(commentId);
        }
    }
}